package com.yanqu.road.server.cmd.wingroom;

import com.yanqu.road.entity.wingroom.WingRoomRegion;
import com.yanqu.road.entity.wingroom.returnentity.DecorateResult;
import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.wingroom.WingRoomModule;
import com.yanqu.road.server.pb.WingRoomPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author ：zfr
 * @date ：2022/2/8 15:26
 */
@Cmd(code = GameProtocol.S_WING_ROOM_PLACE_FURNITURE, desc = "放置家具")
public class PlaceFurnitureCmd extends AbstractCommand {
    private List<WingRoomProto.RegionFurniturePlaceChange> placeChangeList = new ArrayList<>();
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        WingRoomProto.PlaceFurnitureReqMsg reqMsg = WingRoomProto.PlaceFurnitureReqMsg.parseFrom(packet.getBytes());
        placeChangeList = reqMsg.getPlaceChangeList();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DecorateResult result = player.getModule(WingRoomModule.class).furniturePlaceChange(placeChangeList);
        if (result.getErrorFurnitureSet().size() > 0) {
            ret = GameErrorCode.E_WING_ROOM_FURNITURE_PLACE_ERROR;
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        WingRoomProto.PlaceFurnitureRespMsg.Builder respMsg = WingRoomProto.PlaceFurnitureRespMsg.newBuilder();
        respMsg.setRet(ret);
        Set<Integer> changeRegionId = new HashSet<>();
        for (WingRoomProto.RegionFurniturePlaceChange furniturePlaceChange : placeChangeList) {
            changeRegionId.add(furniturePlaceChange.getRegionId());
        }
        for (Integer regionId : changeRegionId) {
            WingRoomRegion region = player.getModule(WingRoomModule.class).getWingRoomData().getRegionMap().get(regionId);
            if (region != null) {
                respMsg.addRegion(WingRoomPb.buildWingRoomRegionTempMsg(region));
            }
        }
        player.sendPacket(Protocol.U_WING_ROOM_PLACE_FURNITURE, respMsg);
    }
}
